package com.sansan.qiangji.controller.base;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.WarehouseAreaAddForm;
import com.sansan.qiangji.form.base.WarehouseAreaEditForm;
import com.sansan.qiangji.form.base.WarehouseAreaListForm;
import com.sansan.qiangji.realize.base.WarehouseAreaReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping("/warehouseArea")
@Slf4j
public class WarehouseAreaController {

    @Autowired
    private WarehouseAreaReal warehouseAreaReal;
    @PostMapping("/addWarehouseArea")
    public ResultVO<Object> addWarehouseArea(@Valid @RequestBody WarehouseAreaAddForm warehouseAreaAddForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品新增】参数不正确，warehouseAreaAddForm={}",warehouseAreaAddForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        warehouseAreaReal.addWarehouseArea(warehouseAreaAddForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/getWarehouseAreaList")
    public ResultVO<Object> getWarehouseAreaList(@Valid @RequestBody WarehouseAreaListForm form, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品查询】参数不正确，warehouseAreaAddForm={}",form);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(warehouseAreaReal.warehouseAreaList(form));
    }

    @PostMapping("/editWarehouseArea")
    public ResultVO<Object> editWarehouseArea(@Valid @RequestBody WarehouseAreaEditForm warehouseAreaEditForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【物品修改】参数不正确，warehouseAreaAddForm={}",warehouseAreaEditForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        warehouseAreaReal.editWarehouseArea(warehouseAreaEditForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/deleteWarehouseArea")
    public ResultVO<Object> deleteWarehouseArea(@RequestParam(value = "warehouseAreaId") Integer warehouseAreaId){
        warehouseAreaReal.deleteWarehouseArea(warehouseAreaId);
        return ResultVOUtil.success(null);
    }
}
